package rabbitmq.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import rabbitmq.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1. 创建连接⼯⼚
        ConnectionFactory factory = new ConnectionFactory();
        //2. 设置参数
        factory.setHost(Constants.HOST);//ip 默认值localhost
        factory.setPort(Constants.PORT); //默认值5672
        factory.setVirtualHost(Constants.VIRTUAL_HOST);//虚拟机名称, 默认 /
        factory.setUsername(Constants.USER_NAME);//⽤⼾名,默认guest
        factory.setPassword(Constants.PASSWORD);//密码, 默认guest
        //3. 创建连接Connection
        Connection connection = factory.newConnection();
        //4. 创建channel通道
        Channel channel = connection.createChannel();
        //5.声明队列
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);
        //6.发送消息
        for (int i = 0; i < 10; i++) {
            String msg = "Hello work queue~" + i;
            channel.basicPublish("",Constants.WORK_QUEUE,null,msg.getBytes());
        }
        //7.资源释放
        channel.close();
        connection.close();
    }
}
